<?php

namespace App\Providers;

use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Monolog\Handler\Mongo;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        DB::listen(function($sql) {
            $sql->sql = str_replace("?", "'%s'", $sql->sql);
            $query = vsprintf($sql->sql, $sql->bindings);
            $ip = app('request')->getClientIp();
            $log = new Logger('SQL');
            $log->pushHandler(
                new \Monolog\Handler\RotatingFileHandler(storage_path('/logs/sql.log'),5)
            );
            $log->info('SQL语句执行：'.$query.'，参数：'.json_encode($sql->bindings,JSON_UNESCAPED_UNICODE).'，ip：'.$ip.'；耗时：'.$sql->time.'ms；');
        });
    }

}
